<?php
namespace App\Api\Models;

use App\Api\Bases\BaseController;
use App\Api\Controllers\Pay\Wxcpay;
use App\Api\Services\JsonService;
use App\Api\Services\Pay\Alipay;
use App\Models\Bargain\CmcPromotionBargainGood;
use Illuminate\Database\Eloquent\Model;

class CmcOrderList extends Model
{
    protected $table = 'cmc_promotions_orderlist';
     /*
      * 查询未付订单模型
      */
    //查找订单
    public static function allOrder($uid)
    {
        return CmcOrderList::where('uid',$uid)->get()->toArray();
    }




    public static  function orderWhere($typeid,$uid,$cartid)
    {
        return self::where(['type'=>$typeid,'paid'=>0,'uid'=>$uid,'cart_id'=>$cartid])->first();
    }
    /*
     * 拼团判断库存
      */
    public static function   judgingInventoryPintu($orderSn)
    {

        $oderlist_num=self::where(['order_sn'=>$orderSn])->first(['oderlist_num','pintuan_id']);
        $promotionPantu=CmcPromotionPintuan::where(['pintuan_id'=>$oderlist_num['pintuan_id']])->first(['pintuan_stock']);

        if ($oderlist_num['oderlist_num']>$promotionPantu['pintuan_stock']){
            return JsonService::fail('库存不足', '', config('code.HTTP_FAIL'));
        }else{
            return JsonService::success('请求成功', '', config('code.HTTP_OK'));
        }
    }

    /*
 * 砍价判断库存
  */
    public static function   judgingInventoryBargain($orderSn)
    {

        $orderBargain=self::where(['order_sn'=>$orderSn])->first(['oderlist_num','bargain_id']);
        $promotionBargain=CmcPromotionBargainGood::where(['bargain_id'=>$orderBargain['bargain_id']])->first(['bargain_stock']);
        if ($orderBargain['oderlist_num']>$promotionBargain['bargain_stock']){
            return JsonService::fail('库存不足', '', config('code.HTTP_FAIL'));
        }else{
            return JsonService::success('请求成功', '', config('code.HTTP_OK'));
        }

    }
    /*
    * 秒杀判断库存
     */
    public static function   judgingInventoryKill($orderSn)
    {
        $orderBargain=self::where(['order_sn'=>$orderSn])->first(['oderlist_num','seckill_id']);

        $promotionBargain=Seckill::where(['id'=>$orderBargain['seckill_id']])->first(['stock']);
        if ($orderBargain['oderlist_num']>$promotionBargain['stock']){
            return JsonService::fail('库存不足', '', config('code.HTTP_FAIL'));
        }else{
            return JsonService::success('请求成功', '', config('code.HTTP_OK'));
        }

    }
    /*
     * 普通购买
     */
    public static function   judgingInventory($orderSn)
    {
        $order=self::where(['order_sn'=>$orderSn])->first(['oderlist_num','goods_id']);
        $promotionBargain=Goods::where(['goods_id'=>$order['goods_id']])->first(['goods_stock']);
        if ($order['oderlist_num']>$promotionBargain['goods_stock']){
            return JsonService::fail('库存不足', '', config('code.HTTP_FAIL'));
        }else{
            return JsonService::success('请求成功', '', config('code.HTTP_OK'));
        }

    }


    //支付宝异步处理
    public static function whereNotifyUrl($uid,$order_sn)
    {
      self::where(['uid'=>$uid,'order_sn'=>$order_sn])->update(['paid'=>1]);
        $orederlist=self::where(['uid'=>$uid,'order_sn'=>$order_sn])->first();
        switch($orederlist['type'])
        {
            case 0:
                //拼团ID
                $pintuan_id=$orederlist['pintuan_id'];
                $PromotionPintuan =CmcPromotionPintuan::where(['pintuan_id'=>$pintuan_id])->first(['pintuan_stock']);
                CmcPromotionPintuan::where(['pintuan_id'=>$pintuan_id])->update(['pintuan_stock'=>$PromotionPintuan['pintuan_stock']-$orederlist['oderlist_num']]);
                break;
            case 1:
                //秒杀ID
                $seckill_id=$orederlist['seckill_id'];
                $PromotionPintuan =Seckill::where(['id'=>$seckill_id])->first(['stock']);
                Seckill::where(['id'=>$seckill_id])->update(['stock'=>$PromotionPintuan['stock']-$orederlist['oderlist_num']]);
                break;
            case 2:
                //砍价ID
                $bargain_id=$orederlist['bargain_id'];
                $PromotionPintuan =CmcPromotionBargainGood::where(['bargain_id'=>$bargain_id])->first(['bargain_stock']);
                CmcPromotionBargainGood::where(['bargain_id'=>$bargain_id])->update(['bargain_stock'=>$PromotionPintuan['bargain_stock']-$orederlist['oderlist_num']]);
                break;
            default:
                return JsonService::fail('传值错误','',config('code.HTTP_FAIL'));
        }
    }
    //判断库存
    static    public  function   CmcOrderListpay($orderSn)
     {
         $orderList=CmcOrderList::where('order_sn',$orderSn)->first(['type']);

         //[拼团0砍价1秒杀2 3]',
         switch($orderList['type'])
         {
             case 0:
                 //查询该订单能否支付（库存）
                 $judgingInventoryPintu=CmcOrderList::judgingInventoryPintu($orderSn);
                 return   $judgingInventoryPintu;
                 break;
             case 1:
                 $judgingInventoryBargain=CmcOrderList::judgingInventoryBargain($orderSn);
                return $judgingInventoryBargain;
                 break;
             case 2:
                 $judgingInventoryKill=CmcOrderList::judgingInventoryKill($orderSn);
                 return  $judgingInventoryKill;
                 break;
             case 3:
                 $judgingInventory=CmcOrderList::judgingInventory($orderSn);
                 return  $judgingInventory;
                 break;
             default:
                 return JsonService::fail('传值错误','',config('code.HTTP_FAIL'));
         }

     }
   /*
    * 选择支付方式
    */
    public static function switchOrder($payType,$orderSn,$totalMoney,$oderlist_name)
    {

        switch($payType)
        {
            case 0:
                $orderList=CmcOrderList::where(['order_sn'=>$orderSn])->first(['uid','total_money']);
                $memberUser=CmcMembers::where(['member_id'=>$orderList['uid']])->first(['balance']);
                if ($orderList['total_money']>$memberUser['balance']) return JsonService::fail('余额不足','',config('code.HTTP_FAIL'));
                CmcMembers::where(['member_id'=>$orderList['uid']])->update(['balance'=>$orderList['total_money']+$memberUser['balance']]);
                return JsonService::success('购买成功', '', config('code.HTTP_OK'));
                break;
            case 1:
                $wxPay=new Wxcpay();
                $wxPay->wxPay( $oderlist_name,$orderSn,$totalMoney);
                echo 1;
                break;
            case 2:
                Alipay::aliIndex($orderSn,$totalMoney,$oderlist_name);
                break;
            case 3:
                return JsonService::fail('此功能正在开发中','',config('code.HTTP_FAIL'));
                break;
            default:
                return JsonService::fail('传值错误','',config('code.HTTP_FAIL'));

        }
    }
      /*
       * 支付宝退款查询金额
       */
    public static function   acheckrefund($out_trade_no)
    {
        //查询订单金额
        $order_sn=self::where(['order_sn'=>$out_trade_no])->first();
         //操作退款
        $upDate=(new BaseController())->TimeFormatting(strtotime("+7 day"));
        if ($order_sn['order_time']>$upDate) return JsonService::fail('需要退款订单超时','',config('code.HTTP_OUT_TIME'));
        Alipay::checkRefund($out_trade_no,$order_sn['total_money']);
        //        退款入库
        CmcOutMoney::typeOutMoney($order_sn);
        CmcOutMoney::outMoney($order_sn);
        return JsonService::success('退款成功','',config('code.HTTP_OK'));
    }

    public static function getList($uid)
    {
        return CmcOrderList::where('uid',$uid)->get()->toArray();
    }

}